package richbar.com.richbar.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.common.base.BaseActivity;
import com.common.base.BaseFragment;
import com.common.util.LogUtil;
import com.common.util.StringUtil;
import com.okhttp.GlideUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import richbar.com.richbar.R;
import richbar.com.richbar.activity.login.LoginAc;
import richbar.com.richbar.activity.me.AccountSetAc;
import richbar.com.richbar.activity.me.CollectAc;
import richbar.com.richbar.activity.me.GoHtmlAc;
import richbar.com.richbar.activity.me.MerchantEntryAc;
import richbar.com.richbar.activity.me.MyEvaluate;
import richbar.com.richbar.activity.me.MyExchangeAc;
import richbar.com.richbar.activity.me.MyQRCodeAc;
import richbar.com.richbar.activity.me.myassets.MyAssetsAc;
import richbar.com.richbar.activity.order.OrderListAc;
import richbar.com.richbar.base.BaseApplication;
import richbar.com.richbar.base.BaseListAdapter;
import richbar.com.richbar.bean.A;
import richbar.com.richbar.bean.UserInfoResult;
import richbar.com.richbar.util.NetWorkUtil;
import richbar.com.richbar.util.SpUtil;
import richbar.com.richbar.util.Util;
import richbar.com.richbar.view.NoScrollGridView;
import richbar.com.richbar.view.linear.LinearLayoutAdapter;
import richbar.com.richbar.view.linear.LinearView;


/**
 * Created by Administrator on 2018/10/13.
 * 我的
 */

public class MineFragment extends BaseFragment implements View.OnClickListener {
    private ImageView head_iv;
    private TextView nickname;
    private TextView member;
    private TextView wait_back_money;
    private TextView available_money;
    private LinearView linearView_bottom;
    private MyBottomAdapter myBottomAdapter;
    private NoScrollGridView gridView_top;
    private MyTopAdapter myTopAdapter;
    private RelativeLayout info_body;
    private ImageView set;
    private TextView logout;
    UserInfoResult.DataEntity.InfoEntity bean;

    @Override
    protected View onCreate(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        head_iv = view.findViewById(R.id.head);
        nickname = view.findViewById(R.id.nickname);
        member = view.findViewById(R.id.member);
        wait_back_money = view.findViewById(R.id.wait_back_money);
        available_money = view.findViewById(R.id.available_money);
        linearView_bottom = view.findViewById(R.id.linearView_bottom);
        gridView_top = view.findViewById(R.id.gridView_top);
        info_body = view.findViewById(R.id.info_body);
        logout = view.findViewById(R.id.logout);
        set = view.findViewById(R.id.set);
        info_body.setOnClickListener(this);
        set.setOnClickListener(this);
        logout.setOnClickListener(this);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initData() {
        getUserInfo();
        List<A> dataTop = new ArrayList<>();
        dataTop.add(new A(R.mipmap.me_01, "待付款"));
        dataTop.add(new A(R.mipmap.me_02, "待发货"));
        dataTop.add(new A(R.mipmap.me_03, "待收货"));
        dataTop.add(new A(R.mipmap.me_04, "售后"));
        dataTop.add(new A(R.mipmap.me_05, "全部订单"));
        myTopAdapter = new MyTopAdapter(getActivity(), dataTop);
        gridView_top.setAdapter(myTopAdapter);
        gridView_top.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 4) {
                    position = 0;
                } else {
                    position++;
                }
                start(position);
            }
        });
        List<A> dataBottom = new ArrayList<>();
        dataBottom.add(new A(R.mipmap.me_list_01, "我的资产"));
        dataBottom.add(new A(R.mipmap.me_list_02, "我的兑换"));
        dataBottom.add(new A(R.mipmap.me_list_03, "我的二维码"));
        dataBottom.add(new A(R.mipmap.me_list_04, "我的评价"));
        dataBottom.add(new A(R.mipmap.me_list_05, "我的收藏"));
        dataBottom.add(new A(R.mipmap.me_list_06, "我要升级"));
        dataBottom.add(new A(R.mipmap.me_list_07, "商家入驻"));
        myBottomAdapter = new MyBottomAdapter(getActivity(), dataBottom);
        linearView_bottom.setAdapter(myBottomAdapter);
        linearView_bottom.setOnItemClickListener(new LinearView.OnItemClickListener() {
            @Override
            public void onItemClicked(View v, int position) {
                switch (position) {
                    case 0:
                        start(MyAssetsAc.class);
                        break;
                    case 1:
                        start(MyExchangeAc.class);
                        break;
                    case 2:
                        start(MyQRCodeAc.class);
                        break;
                    case 3:
                        start(MyEvaluate.class);
                        break;
                    case 4:
                        start(CollectAc.class);
                        break;
                    case 5:
                        // IntentUtil.toWeb(getActivity(),NetWorkUtil.APK_URL);
                        GoHtmlAc.start(getActivity(), NetWorkUtil.URL + "/home/upgrade/index&uid=" + BaseApplication.uid, "我要升级");
                        break;
                    case 6:
                        start(MerchantEntryAc.class);
                        break;
                }
            }
        });
        head_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AccountSetAc.class);
                MineFragment.this.startActivityForResult(intent, 104);
            }
        });
    }


    private void getUserInfo() {
        NetWorkUtil.getInfo(BaseApplication.uid, Util.md5(), new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                final UserInfoResult result = MyGson.getInstance().fromJson(data, UserInfoResult.class);
                if (checkIsOk(result)) {
                    setUserInfo(result);
                }
            }
        });
    }

    private void setUserInfo(UserInfoResult result) {
        bean = result.getData().getInfo();
        nickname.setText(StringUtil.isEmpty(bean.getNickname()) ? "" : bean.getNickname());
        member.setText(bean.getLvlName());
        wait_back_money.setText("待返金额" + bean.getBalance());
        available_money.setText("可用金额" + bean.getBalance());
        GlideUtil.loadCircleImage(getActivity(), bean.getHead(), head_iv);
        BaseApplication.isSetPayPwd = result.getData().getInfo().getSetpaypwd();
    }

    private void start(final int position) {
        start(OrderListAc.class, new BaseActivity.OnBundleListener() {
            @Override
            public Bundle onSetBundle(Bundle bundle) {
                bundle.putInt("pos", position);
                return bundle;
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.info_body:
                //start(PersonInfoAc.class);
                break;
            case R.id.set:
                Intent intent = new Intent(getActivity(), AccountSetAc.class);
                MineFragment.this.startActivityForResult(intent, 104);
                break;
            case R.id.logout:
                 SpUtil.clearLoginInfo(getActivity());
                 BaseApplication.uid="";
                 BaseApplication.isLogout=false;
                 LoginAc.start(getActivity());
                break;
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 104 && resultCode == getActivity().RESULT_OK) {
            LogUtil.e("OODOOWOADAWD", "111111");
            getUserInfo();
        }

    }

    class MyTopAdapter extends BaseListAdapter<A> {
        public MyTopAdapter(Context context, List<A> list) {
            super(context, list);
        }

        @Override
        public View convert(int i, View view, ViewGroup viewGroup, A bean) {
            View v = getLayoutInflater().inflate(R.layout.item_top_mine, null);
            ImageView iv = v.findViewById(R.id.iv);
            TextView tv = v.findViewById(R.id.tv);
            iv.setImageResource(bean.getImgRes());
            tv.setText(bean.getTitle());
            return v;
        }
    }

    class MyBottomAdapter extends LinearLayoutAdapter<A> {
        public MyBottomAdapter(Context context, List<A> list) {
            super(context, list);
        }

        @Override
        protected View getView(List<A> data, A bean, final int position) {
            View v = getLayoutInflater().inflate(R.layout.item_mine, null);
            ImageView iv = v.findViewById(R.id.iv);
            TextView tv = v.findViewById(R.id.tv);
            View line = v.findViewById(R.id.line);
            iv.setImageResource(bean.getImgRes());
            tv.setText(bean.getTitle());
            if (position == 6) {
                line.setVisibility(View.INVISIBLE);
            }
            return v;
        }
    }

    public byte[] stringtoBitmapbyte(String string) {
        //将字符串转换成Bitmap类型
        byte[] bitmapArray = null;
        try {

            bitmapArray = Base64.decode(string, Base64.DEFAULT);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return bitmapArray;
    }
}

